package com.dingtalk.demo.biz.query;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by well on 17/7/17.
 */
public class BaseQuery {

    private int maxShowPage = 5;
    private int pageSize = 10;
    private int startRow = 0;
    private int page = 1;
    private int totalCount;
    private List<Integer> pages = new ArrayList();

    public BaseQuery() {
    }

    public int getPageSize() {
        return this.pageSize;
    }

    public void setPageSize(int pageSize) {
        if(pageSize <= 1) {
            pageSize = 10;
        }

        this.startRow = (this.page - 1) * pageSize;
        this.pageSize = pageSize;
    }

    public int getStartRow() {
        return this.startRow;
    }

    public int getPage() {
        return this.page;
    }

    public void setPage(int page) {
        if(page <= 1) {
            page = 1;
        }

        this.startRow = (page - 1) * this.pageSize;
        this.page = page;
    }

    public int getMaxPage() {
        return this.totalCount <= 1?1:(this.pageSize <= 1?this.totalCount:(this.totalCount + this.pageSize - 1) / this.pageSize);
    }

    public int getPageCount() {
        return this.getMaxPage();
    }

    public boolean hasNextPage() {
        int currentPage = this.getCurrentPage();
        return currentPage < this.getMaxPage();
    }

    public boolean hasPrevPage() {
        int currentPage = this.getCurrentPage();
        return currentPage > 1;
    }

    public int getNextPage() {
        int currentPage = this.getCurrentPage();
        return this.hasNextPage()?currentPage + 1:currentPage;
    }

    public int getPrevPage() {
        int currentPage = this.getCurrentPage();
        return this.hasPrevPage()?currentPage - 1:currentPage;
    }

    public boolean turnNext() {
        if(this.hasNextPage()) {
            this.page = this.getNextPage();
            return true;
        } else {
            return false;
        }
    }

    public boolean turnPrev() {
        if(this.hasPrevPage()) {
            this.page = this.getPrevPage();
            return true;
        } else {
            return false;
        }
    }

    public boolean turn(int page) {
        int maxPage = this.getMaxPage();
        if(page > maxPage) {
            this.page = maxPage;
            return false;
        } else if(page < 1) {
            this.page = 1;
            return false;
        } else {
            this.page = page;
            return true;
        }
    }

    public int getCurrentPage() {
        int maxPage = this.getMaxPage();
        return this.page > maxPage?maxPage:(maxPage < 1?1:this.page);
    }

    public int getTotalCount() {
        return this.totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
        int page = (totalCount + this.pageSize - 1) / this.pageSize + 1;
        if(this.page > page) {
            this.page = page;
        }

        this.countPages();
    }

    private void countPages() {
        int page = this.getMaxPage();
        if(page <= this.maxShowPage) {
            for(int prefix = 1; prefix <= page; ++prefix) {
                this.pages.add(Integer.valueOf(prefix));
            }
        } else {
            ArrayList var6 = new ArrayList();
            ArrayList suffix = new ArrayList();
            int size;
            if(this.page < 3) {
                for(size = 1; size < this.page + 1; ++size) {
                    var6.add(Integer.valueOf(size));
                }
            } else if(page - this.page == 0) {
                for(size = 4; size > -1; --size) {
                    if(this.page - size > 0) {
                        var6.add(Integer.valueOf(this.page - size));
                    }
                }
            } else if(page - this.page == 1) {
                for(size = 3; size > -1; --size) {
                    if(this.page - size > 0) {
                        var6.add(Integer.valueOf(this.page - size));
                    }
                }
            } else {
                var6.add(Integer.valueOf(this.page - 2));
                var6.add(Integer.valueOf(this.page - 1));
                var6.add(Integer.valueOf(this.page));
            }

            size = this.maxShowPage - var6.size();

            for(int i = this.page + 1; i <= page && i <= this.page + size; ++i) {
                suffix.add(Integer.valueOf(i));
            }

            this.pages.addAll(var6);
            this.pages.addAll(suffix);
        }

    }

    public int getMaxShowPage() {
        return this.maxShowPage;
    }

    public void setMaxShowPage(int maxShowPage) {
        this.maxShowPage = maxShowPage;
    }

    public List<Integer> getPages() {
        return this.pages;
    }

    public void setPages(List<Integer> pages) {
        this.pages = pages;
    }

    public boolean equals(Object o) {
        if(this == o) {
            return true;
        } else if(!(o instanceof BaseQuery)) {
            return false;
        } else {
            BaseQuery baseQuery = (BaseQuery)o;
            if(this.maxShowPage != baseQuery.maxShowPage) {
                return false;
            } else if(this.page != baseQuery.page) {
                return false;
            } else if(this.pageSize != baseQuery.pageSize) {
                return false;
            } else if(this.startRow != baseQuery.startRow) {
                return false;
            } else if(this.totalCount != baseQuery.totalCount) {
                return false;
            } else {
                if(this.pages != null) {
                    if(!this.pages.equals(baseQuery.pages)) {
                        return false;
                    }
                } else if(baseQuery.pages != null) {
                    return false;
                }

                return true;
            }
        }
    }

    public int hashCode() {
        int result = this.maxShowPage;
        result = 31 * result + this.pageSize;
        result = 31 * result + this.startRow;
        result = 31 * result + this.page;
        result = 31 * result + this.totalCount;
        result = 31 * result + (this.pages != null?this.pages.hashCode():0);
        return result;
    }
}
